﻿using System;
using System.Collections.Generic;
using UMC.Data;
using UMC.ITME.Entities;
using UMC.Net;
using UMC.Web;
using UMC.Web.UI;

namespace UMC.ITME.Activities
{
    [Apiumc("ITME", "Fingerprint", Auth = WebAuthType.Admin, Desc = "可信客户端")]
    class SiteFingerprintActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {


            var Fingerprint = this.AsyncDialog("Fingerprint", r => this.Prompt("请输入应用指纹"));




            var bytes = Utility.FromBase64String(Fingerprint);
            var fid = new Net.StringValue(bytes);
            var model = this.AsyncDialog("Model", akey =>
            {

                var fire = DataFactory.Instance().Firewall(new Net.StringValue(bytes));

                var ui = UISection.Create(new UITitle("应用指纹"));
                ui.AddCell("指纹", Fingerprint);
                switch (fire?.AuthType ?? AuthType.Check)
                {
                    case AuthType.Deny:
                        {
                            ui.AddCell("访问许可", "拒绝");
                            UIDesc desc = new UIDesc("应用指纹已经拒绝，无须再配置用户代理");
                            desc.Desc("{icon}\n{desc}").Put("icon", "\uf24a");
                            desc.Style.Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60));
                            ui.NewSection().Add(desc);
                        }
                        break;
                    case AuthType.Allow:
                        {
                            ui.AddCell("访问许可", "允许");
                            UIDesc desc = new UIDesc("应用指纹已经允许，无须再配置用户代理");
                            desc.Desc("{icon}\n{desc}").Put("icon", "\uf24a");
                            desc.Style.Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60));
                            ui.NewSection().Add(desc);
                        }
                        break;
                    default:
                        ui.AddCell("访问许可", "检验");
                        var clients = HotCache.Find(new Fingerprint { Id = fid });
                        foreach (var client in clients)
                        {
                            switch (client.AuthType ?? AuthType.Check)
                            {
                                case AuthType.Check:
                                    ui.NewSection().Add(new UIDesc(client.UserAgent.Span.UTF8())).AddCell(client.Title.Span.UTF8(), "检验", new UIClick(request, akey, "More", "UA", client.UA.ToString()));
                                    break;
                                case AuthType.Allow:
                                    ui.NewSection().Add(new UIDesc(client.UserAgent.Span.UTF8())).AddCell(client.Title.Span.UTF8(), "允许", new UIClick(request, akey, "More", "UA", client.UA.ToString()));//.Add(new UIDesc(client.UserAgent.Span.UTF8()));
                                    break;
                                case AuthType.Deny:
                                    ui.NewSection().Add(new UIDesc(client.UserAgent.Span.UTF8())).AddCell(client.Title.Span.UTF8(), "拒绝", new UIClick(request, akey, "More", "UA", client.UA.ToString()));//.Add(new UIDesc(client.UserAgent.Span.UTF8()));
                                    break;
                            }
                        }

                        if (clients.Length == 0)
                        {
                            UIDesc desc = new UIDesc("未设置此应用指纹用户代理");
                            desc.Desc("{icon}\n{desc}").Put("icon", "\uf24a");
                            desc.Style.Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60));
                            ui.NewSection().Add(desc);

                        }


                        ui.UIFootBar = new UIFootBar() { IsFixed = true };
                        ui.UIFootBar.AddText(new UIEventText("新增代理").Click(new UIClick(request, akey, "ADD")),
                            new UIEventText("指纹许可").Click(new UIClick(request, akey, "Finger")).Style(new UIStyle().BgColor()));
                        break;
                }
                ui.SendTo(this.Context, $"{request.Model}.{request.Command}");


            });
            switch (model)
            {
                case "Finger":
                    {
                        switch (this.AsyncDialog("Auth", r =>
                        {
                            return new UISheetDialog() { Title = "应用指纹访问安全" }
                            .Put("允许", "Allow").Put("拒绝", "Deny").Put("检验", "Check");

                        }))//.UTF8());
                        {

                            case "Allow":
                                this.Context.Loger.Info().Write("设置了允许").Write(Fingerprint).Write("访问").End();
                                HotCache.Put(new Firewall { Fingerprint = fid, AuthType = AuthType.Allow, Type = 1 });

                                Utility.SyncClient(4, fid, new StringValue(new byte[] { 1 }));
                                break;
                            case "Deny":
                                this.Context.Loger.Info().Write("设置了拒绝").Write(Fingerprint).Write("访问").End();
                                HotCache.Put(new Firewall { Fingerprint = fid, AuthType = AuthType.Deny, Type = 1 });

                                // SiteFirewallActivity.Async(fid, 2);
                                Utility.SyncClient(4, fid, new StringValue(new byte[] { 2 }));
                                break;
                            case "Check":
                                this.Context.Loger.Info().Write("还原了").Write(Fingerprint).Write("访问").End();
                                HotCache.Delete(new Firewall { Fingerprint = fid });
                                // SiteFirewallActivity.Async(fid, 0);

                                Utility.SyncClient(4, fid);
                                break;
                        }
                        this.Context.Send(true);
                    }
                    break;
                case "ADD":
                    var value = this.AsyncDialog("UserAgent", r =>
                           {
                               var fm = new UIFormDialog() { Title = "新增代理" };
                               fm.AddText("应用名称", "Title", String.Empty);
                               fm.AddTextarea("代理字符串", "UserAgent", String.Empty).PlaceHolder("应用或浏览器的User-Agent").Put("tip", "浏览器的User-Agent");
                               // fm.AddRadio("发送方式", "method").Put("POST", "POST", provider["method"] == "POST").Put("PUT", "PUT", provider["method"] == "PUT");
                               fm.Submit("确认", $"{request.Model}.{request.Command}");
                               return fm;
                           });
                    var UserAgent = new StringValue(value["UserAgent"].UTF8());
                    var Title = new StringValue(value["Title"].UTF8());

                    HotCache.Put(new Fingerprint { Id = fid, Title = Title, AuthType = AuthType.Check, UA = Utility.UserAgent(UserAgent.Span), UserAgent = UserAgent });
                    this.Context.Send(true);
                    break;
                case "More":
                    var UA = Utility.IntParse(this.AsyncDialog("UA", "0"), 0);
                    switch (this.AsyncDialog("Mofe", r => new UISheetDialog() { Title = "代理访问安全" }
                            .Put("允许", "Allow").Put("拒绝", "Deny").Put("检验", "Check")))//.UTF8());
                    {
                        case "Allow":
                            HotCache.Put(new Fingerprint { Id = fid, AuthType = AuthType.Allow, UA = UA });
                            this.Context.Loger.Info().Write($"许可了{Fingerprint}指纹的{UA}代理的访问");

                            break;
                        case "Deny":
                            HotCache.Put(new Fingerprint { Id = fid, AuthType = AuthType.Deny, UA = UA });
                            this.Context.Loger.Info().Write($"拒绝了{Fingerprint}指纹的{UA}代理的访问");
                            // this.Context.Loger.Info().Write($"拒绝了{UA}的授权");
                            break;
                        case "Check":
                            HotCache.Put(new Fingerprint { Id = fid, AuthType = AuthType.Check, UA = UA });
                            this.Context.Loger.Info().Write($"还原了{Fingerprint}指纹的{UA}代理的访问");
                            // this.Context.Loger.Info().Write($"还原了{UA}的授权");
                            break;
                    }
                    this.Context.Send(true);
                    break;
            }



        }
    }
}